<?php 
  include "inc/conn.php";
  include "config.php";
  $tts = date("YmdHis",time());
  // $tts = date("Y-m-d H:i:s",time());
// $arr_sucess = array("code" =>"1" ,"msg"=>"签到成功" );

function json_encodeX($array) {
	if(version_compare(PHP_VERSION,'5.4.0','<')){
	  $str = json_encode($array);
	  $str = preg_replace_callback("#\\\u([0-9a-f]{4})#i",function($matchs){
	       return iconv('UCS-2BE', 'UTF-8', pack('H4', $matchs[1]));
	  },$str);
	  return $str;
	}else{
	  return json_encode($array, JSON_UNESCAPED_UNICODE);
	}
}

$stime=microtime(true); 
$time_creat = date("Y-m-d H:i:s",time());
$codes = trim($_POST['code']);
$shujus = $_POST['txt_data'];
$shuru1 = $_POST['name'];
if($shujus == 1){
/*
  if($ismas=="1"){
    session_start();
    if($codes!=$_SESSION['PHP_M2T']){
      webalert("请正确输入验证码！");
    }
  }
  if(!$shuru1 || empty($shuru1)){
    webalert("请输入$tiaojian1!");
  }

  if(!file_exists($file_qd_all)){
    webalert('请检查数据库文件');
  }
*/

  $filer = fopen($file_qd_all, "r") or  webalert('无法打开文件!');
  $a_files = file($file_qd_ok);
  $r = array();
  for($i=0; $i<count($a_files); $i++) {
    $t = str_getcsv($a_files[$i], "\t");
    $r[] = $t[1];
  }

while (!feof($filer)) {
    $ii++;
    $rows = fgets($filer);
    $rows = trim($rows);
    if ($rows) {

            $tabvs = explode("\t", $rows);
            //print_r($tabvs);
            if ("_" . $shuru1 == "_" . $tabvs[1]) {
            	
                // echo "<!-- $shuru1 == $tabvs[1] -->";
                // 查询成功后，将数据存入文件，并且提示签到成功，这样能知道签到有多少人
                $fp_qd_ok=fopen($file_qd_ok,"a+b");
                $fp_time=fopen($file_qd_time,"a+b");
                
                if( in_array($shuru1, $r) ) {
                	$arr_sucess = array("code"=>"1","msg"=>"签到失败" , "success_msg"=>"请勿重复签到" ,"success_msg_repeat"=>"提示：重复签到" ,"user_name" =>$tabvs[0], "user_phone"=>$shuru1);
                	// echo "arr_sucess";
                	echo json_encodeX($arr_sucess);
                	exit;
	            } else {
                  fwrite($fp_qd_ok,"$tabvs[0]\t{$shuru1}\r\n"); 
                  fwrite($fp_time,"{$time_creat}\r\n"); 

                }
                fclose($fp);
                fclose($fp_time);
                $iae++;
                $iaf = 0;
                $ddd_a = $tabvs[$iaf];
                $ddd_b = $shuru1;
                
                
            }
    }
}

if($iae<1){
    // echo "签到失败";
  $arr_error = array("code"=>"0", "msg"=>"签到失败" , "success_msg"=>$failed_msg ,"success_msg_repeat"=>"签到失败","user_name" =>"暂无数据", "user_phone"=>$shuru1);
    echo json_encodeX($arr_error);
    exit;
} else {
  $arr_sucess = array("code"=>"2", "msg"=>"签到成功" , "success_msg"=>$success_msg ,"success_msg_repeat"=>"首次签到","user_name" =>$ddd_a, "user_phone"=>$ddd_b);
  echo json_encodeX($arr_sucess);
  exit;

}

fclose($filer); 

}

$etime=microtime(true);//获取程序执行结束的时间
$total=$etime-$stime;   //计算差值
//echo "<div>页面执行时间：{$total} 秒</div>";

?>
